package com.lyf.baoxian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.lyf.baoxian.R;
import com.lyf.baoxian.activity.BuyTaoCanActivity;
import com.lyf.baoxian.activity.SearchActivity;
import com.lyf.baoxian.activity.SelectAdsActivity;
import com.lyf.baoxian.activity.ShowTaoCanActivity;
import com.lyf.baoxian.adapter.MyViewPagerAdapter;
import com.lyf.baoxian.adapter.TaoCanAdapter;
import com.lyf.baoxian.adapter.TaoCanAdapter2;
import com.lyf.baoxian.bean.TestTC;
import com.lyf.baoxian.utils.Dp2pxUtil;
import com.lyf.baoxian.view.MyListView;
import com.xys.libzxing.zxing.activity.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

/**
 * Created by 刘亚飞 on 2016/11/30.
 * 主界面
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

  /*  @Bind(R.id.viewpager)
    ViewPager mViewPager;*/

    @Bind(R.id.iv_toutu)
    ImageView iv_toutu;

   /* @Bind(R.id.ll_dots)
    LinearLayout ll_dots;*/

    @Bind(R.id.iv_scan)
    ImageView iv_scan;

    @Bind(R.id.home_rg)
    RadioGroup rg_home;

    @Bind(R.id.home_zc)
    RadioButton home_zc;

    @Bind(R.id.home_fzc)
    RadioButton home_fzc;

    @Bind(R.id.lv_home)
    MyListView lv_home;

    @Bind(R.id.scrol)
    ScrollView scrollView;

    //搜索
    @Bind(R.id.tv_search)
    TextView tv_search;

    //选择地址
    @Bind(R.id.tv_address)
    TextView tv_address;

    //政策保险
    private TaoCanAdapter adapter;
    //非政策保险
    private TaoCanAdapter2 adapter2;

    List<TestTC> data1;
    List<TestTC> data2;

   /* @Bind(R.id.lv_taocan)
    ListView mListView;*/

    private static final int SHOW_NEXT_PAGER = 1;

   /* private int[] imageResIds = {
            R.drawable.a,
            R.drawable.b,
            R.drawable.c,
            R.drawable.d
    };*/

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_NEXT_PAGER:
//                    showNextPager();
            }
        }
    };

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        scrollView.smoothScrollTo(0, 0);
        iv_scan.setOnClickListener(this);
        rg_home.setOnCheckedChangeListener(this);
        tv_search.setOnClickListener(this);
        tv_address.setOnClickListener(this);
        /*mViewPager.setOnPageChangeListener(changeListener);
        mViewPager.setAdapter(new MyViewPagerAdapter(imageResIds));*/
        Log.i("MY", "afterCreate");
//        Log.i("MY", "wede" + ll_dots.getChildCount());

        initData();
//        initDot();
        //默认选择0页
//        changeDotandDesc(0);
        //滑到一半的地方
      /*  mViewPager.setCurrentItem(mViewPager.getAdapter().getCount() / 2);
        //3秒后显示下一页
        handler.sendEmptyMessageDelayed(SHOW_NEXT_PAGER, 3000);*/

    }

    @Override
    protected View addLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        return view;
    }

    /*ViewPager.OnPageChangeListener changeListener = new ViewPager.OnPageChangeListener() {
        *//**
         * 页面滑动到时候调用的方法
         * @param position
         * @param positionOffset
         * @param positionOffsetPixels
         *//*
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        *//**
         * 当页面被选择的时候调用的方法
         *//*
        @Override
        public void onPageSelected(int position) {
            changeDotandDesc(position);
        }

        *//**
         * 当页面滑动状态改变之后调用的方法
         * @param state
         *//*
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };*/

    /**
     * 改变指定位置的描述和点
     *
     * @param position
     */
   /* private void changeDotandDesc(int position) {
        position = position % ll_dots.getChildCount();
        for (int i = 0; i < ll_dots.getChildCount(); i++) {
            //把指定位置的点设置成选择状态，其他的设置为未选择状态
            ll_dots.getChildAt(i).setSelected(i == position);
        }
    }*/

    /**
     * 初始化ViewPager底部的点
     */
    /*private void initDot() {
        for (int i = 0; i < imageResIds.length; i++) {
            int myDp = Dp2pxUtil.dp2px(5, mActivity);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(myDp, myDp);
            params.leftMargin = myDp;
            View dot = new View(mActivity);
            dot.setLayoutParams(params);
            dot.setBackgroundResource(R.drawable.shape_dot);
            ll_dots.addView(dot);
            Log.i("MY", "===" + ll_dots.getChildCount());
        }
        Log.i("MY", "" + imageResIds.length);


    }
*/
    /**
     * 显示下一页
     */
    /*private void showNextPager() {
        //获取当前显示的是哪一页
        int currentItem = mViewPager.getCurrentItem();
        mViewPager.setCurrentItem(currentItem + 1);
        handler.sendEmptyMessageDelayed(SHOW_NEXT_PAGER, 3000);
    }*/

    @Override
    public void onDestroy() {
        Log.i("MY", "onDestroy");
        handler.removeCallbacksAndMessages(null);
        ButterKnife.unbind(this);
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_scan: //二维码扫描
                Intent intent = new Intent(mActivity, CaptureActivity.class);
                startActivityForResult(intent, 111);
                break;

            case R.id.tv_search: //跳到搜索界面
                Intent intent1 = new Intent(mActivity, SearchActivity.class);
                startActivity(intent1);
                break;
            case R.id.tv_address: //选择地址
                Intent intent2 = new Intent(mActivity, SelectAdsActivity.class);
                startActivityForResult(intent2,122);
                break;
        }

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 111:
                if (resultCode == RESULT_OK) {
                    //二维码扫描成功

                }
                break;

            case 122:
               if (resultCode == 1001) {
                String city = data.getStringExtra("city");
                tv_address.setText(city);
               }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }



    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.home_zc: //政策保险
                if (adapter == null) {
                    adapter = new TaoCanAdapter(mActivity);
                }
                adapter.setDatas(data1);
                lv_home.setAdapter(adapter);
                rg_home.setBackgroundResource(R.drawable.left);
                break;

            case R.id.home_fzc: //非政策保险
                if (adapter2 == null) {
                    adapter2 = new TaoCanAdapter2(mActivity);
                }
                adapter2.setDatas(data2);
                lv_home.setAdapter(adapter2);
                rg_home.setBackgroundResource(R.drawable.right);
                break;
        }

    }


    /**
     * 初始化数据
     */
    private void initData() {
        data1 = new ArrayList<>();
        data2 = new ArrayList<>();

        data1.add(new TestTC(R.drawable.shuidao,"水稻种植成本保险","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
        data1.add(new TestTC(R.drawable.yumi,"玉米种植成本保险","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));

        data2.add(new TestTC(R.drawable.shuidao,"水稻种植成本保险","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
        data2.add(new TestTC(R.drawable.yumi,"玉米种植成本保险","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
       /* for (int i = 0; i < 10; i++) {
            data1.add(new TestTC(R.drawable.shuidao,"水稻","属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
            data2.add(new TestTC(R.drawable.lv1,"大豆套餐"+i,"属于政策性保险，政府保费补贴高，农户自交保费少，保障范围广。"));
        }*/


        adapter = new TaoCanAdapter(mActivity);
        adapter.setDatas(data1);
        lv_home.setAdapter(adapter);
        rg_home.check(R.id.home_zc);
        rg_home.setOnCheckedChangeListener(this);
        lv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(mActivity,"位置w"+i,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mActivity, BuyTaoCanActivity.class);
                startActivity(intent);
            }
        });
    }

}
